/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: message.h
 *
 * Purpose: define communication message data struct
 *
 * Developer:
 *   wen.gu , 2020-04-26
 *
 * TODO:
 *
 ***************************************************************************/

#ifndef __ARA_COM_RUNTIME_MESSAGE_H__
#define __ARA_COM_RUNTIME_MESSAGE_H__
/******************************************************************************
 **    INCLUDES
 ******************************************************************************/
#include <cstdint>
#include <memory>

#include "ara/core/string.h"
/******************************************************************************
 **    MACROS
 ******************************************************************************/


/******************************************************************************
 **    TYPE DEFINITIONS
 ******************************************************************************/
namespace ara
{
namespace com
{
namespace runtime
{
enum class MessageType: uint8_t
{
    kEvent = 0x0, /** event message (skeleton -> proxy) */
    kEventSubscribe = 0x01, /** proxy subscirbe a event from skeleton (proxy -> skeleton) */
    kEventSubscribeAck = 0x02, /** return the subscribe state (skeleton -> proxy) */
    kEventUnsubscribe = 0x03, /** unsubscribe a event (proxy -> skeleton) */
    kEventUnsubscribeAck = 0x04, /** return unsubscribe state (skeleton -> proxy */

    kFeildGet = 0x10,     /** get feild value from skeleton (proxy -> skeleton) */
    kFeildGetAck = 0x11,  /** return got value from skeleton feild (skeleton - > proxy) */
    kFeildSet = 0x12,    /** set a value to skeleton feild (proxy -> skeleton) */
    kFeildSetAck = 0x13, /** return the new value after feild set (skeleton - > proxy) */
    kFeildNotify = 0x14, /** notify feild changed value (skeleton -> proxy) */ 
    kRequest = 0x20,  /** request a method call (proxy -> skeleton) */
    kResponse = 0x21, /** return the result of after method call (skeleton -> proxy) */  

    kError = 0x91, /** error from skeleton to proxy (skeleton -> proxy) */    
};
/******************************************************************************
 **    CLASSES/FUNCTIONS DEFINITIONS
 ******************************************************************************/
class Message
{
public:
    using PayloadPtr = std::shared_ptr<uint8_t>;  
public:
    Message() = default;
    virtual ~Message() = default;
public:
    /** getters */
    virtual MessageType Type() const { return mType; }
    virtual uint32_t ClientId() const { return mClientId; }
    virtual uint32_t SessionId() const { return mSessionId; }
    virtual uint32_t PayloadSize() const { return mPayloadSize; }
    virtual PayloadPtr Payload() const { return mPayload; }
    virtual const ara::core::String& Name() const { return mName; }
    virtual const ara::core::String& BindingType() const { return mBindingType; }
  
    /** setters */
    virtual void SetType(MessageType type) { mType = type; }
    virtual void SetClientId(uint32_t id) { mClientId = id; }
    virtual void SetSessionId(uint32_t id) { mSessionId = id; }
    virtual void SetPayloadSize(uint32_t size) { mPayloadSize = size; }
    virtual void SetPayload(PayloadPtr payload) { mPayload = payload; }
    virtual void SetName(const ara::core::String& name) { mName = name; }
    virtual void SetBindingType(const ara::core::String& bindingType) { mBindingType = bindingType; }
protected:
    MessageType mType = MessageType::kError; /**todo, refineme?? what default type set as */
    /** if skeleton endian, indicate that where is the message come from */
    uint32_t mClientId = 0;
    uint32_t mSessionId = 0; /**like packet id, a unique sequence */ 

    uint32_t mPayloadSize = 0;
    PayloadPtr mPayload = nullptr;

    ara::core::String mName; /**message name(e.g. method name for method, event name for event) */
    ara::core::String mBindingType; /** current message transfer by the this binding type */
};


/** for message shared pointer type */
using MessagePtr = std::shared_ptr<Message>;

} /** namespace runtime */
} /** namespace com */
} /** namespace ara */

#endif /** !__ARA_COM_RUNTIME_MESSAGE_H__ */

